//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Activity;
import com.example.entity.Activityapplication;
import com.example.mapper.ActivityMapper;
import com.example.service.ActivityapplicationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import jakarta.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

import java.util.Iterator;
import java.util.List;

@Service
public class ActivityService {
    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private ActivityapplicationService activityapplicationService;

    private final ObjectMapper objectMapper = new ObjectMapper();

    public ActivityService() {
    }

    // 获取当前登录学生的ID
    private Integer getCurrentStudentId() {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                String encodedUser = request.getHeader("user");
                if (encodedUser != null) {
                    // 解码Base64并解析JSON
                    String decodedUser = java.net.URLDecoder.decode(
                        new String(java.util.Base64.getDecoder().decode(encodedUser)), 
                        "UTF-8"
                    );
                    Map<String, Object> user = objectMapper.readValue(decodedUser, Map.class);
                    if (user != null && "STU".equals(user.get("role"))) {
                        return (Integer) user.get("id");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void add(Activity activity) { //管理员不需要注册只需要新增，只有管理员才能增加管理员
        this.activityMapper.insert(activity);
    }

    public void updateById(Activity activity) {
        this.activityMapper.updateById(activity);
    }

    public void deleteById(Integer id) {
        this.activityMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        Iterator var2 = ids.iterator();

        while(var2.hasNext()) {
            Integer id = (Integer)var2.next();
            this.deleteById(id);
        }

    }

    public Activity selectById(Integer id) {
        return this.activityMapper.selectById(id);
    }

    public List<Activity> selectAll(Activity activity) {
        List<Activity> list = this.activityMapper.selectAll(activity);
        Integer currentStudentId = getCurrentStudentId();

        if (currentStudentId != null) {
            for (Activity act : list) {
                // 调用 checkApplicationExistsForActivity 获取报名记录
                Activityapplication application = this.activityapplicationService.checkApplicationExistsForActivity(act.getId(), currentStudentId);
                // 如果存在报名记录，将状态赋值给活动的 applicationStatus 字段
                if (application != null) {
                    act.setApplicationStatus(application.getStatus());
                } else {
                    act.setApplicationStatus(null); // 或者设置为一个表示未报名的特定值
                }
            }
        }
        return list;
    }

    public List<Activity> selectList(Activity activity) {
        System.out.println(activity);
        return null;
    }

    public PageInfo<Activity> selectPage(Activity activity, Integer pageNum, Integer pageSize, Integer presidentId) {
        PageHelper.startPage(pageNum, pageSize);

        List<Activity> list = activityMapper.selectByPresidentId(activity,presidentId);
        
        Integer currentStudentId = getCurrentStudentId();

        if (currentStudentId != null) {
            for (Activity act : list) {
                // 调用 checkApplicationExistsForActivity 获取报名记录
                Activityapplication application = this.activityapplicationService.checkApplicationExistsForActivity(act.getId(), currentStudentId);
                // 如果存在报名记录，将状态赋值给活动的 applicationStatus 字段
                if (application != null) {
                    act.setApplicationStatus(application.getStatus());
                } else {
                    act.setApplicationStatus(null); // 或者设置为一个表示未报名的特定值
                }
            }
        }

        return PageInfo.of(list);
    }
    
}
